package com.ruoyi.work.controller;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import com.ruoyi.work.domain.MapRoadbooks;
import com.ruoyi.work.service.IMapRoadbooksService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 路书Controller
 * 
 * @author ruoyi
 * @date 2025-09-28
 */
@Tag(name = "路书")
@RestController
@RequestMapping("/work/roadbooks")
public class MapRoadbooksController extends BaseController
{
    @Autowired
    private IMapRoadbooksService mapRoadbooksService;

    /**
     * 查询路书列表
     */
    @Operation(summary = "查询路书列表")
    @PreAuthorize("@ss.hasPermi('work:roadbooks:list')")
    @GetMapping("/list")
    public TableDataInfo list(MapRoadbooks mapRoadbooks)
    {
        startPage();
        List<MapRoadbooks> list = mapRoadbooksService.selectMapRoadbooksList(mapRoadbooks);
        return getDataTable(list);
    }

    /**
     * 导出路书列表
     */
    @Operation(summary = "导出路书列表")
    @PreAuthorize("@ss.hasPermi('work:roadbooks:export')")
    @Log(title = "路书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MapRoadbooks mapRoadbooks)
    {
        List<MapRoadbooks> list = mapRoadbooksService.selectMapRoadbooksList(mapRoadbooks);
        ExcelUtil<MapRoadbooks> util = new ExcelUtil<MapRoadbooks>(MapRoadbooks.class);
        util.exportExcel(response, list, "路书数据");
    }

    /**
     * 获取路书详细信息
     */
    @Operation(summary = "获取路书详细信息")
    @PreAuthorize("@ss.hasPermi('work:roadbooks:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mapRoadbooksService.selectMapRoadbooksById(id));
    }

    /**
     * 新增路书
     */
    @Operation(summary = "新增路书")
    @PreAuthorize("@ss.hasPermi('work:roadbooks:add')")
    @Log(title = "路书", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MapRoadbooks mapRoadbooks)
    {
        return toAjax(mapRoadbooksService.insertMapRoadbooks(mapRoadbooks));
    }

    /**
     * 修改路书
     */
    @Operation(summary = "修改路书")
    @PreAuthorize("@ss.hasPermi('work:roadbooks:edit')")
    @Log(title = "路书", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MapRoadbooks mapRoadbooks)
    {
        return toAjax(mapRoadbooksService.updateMapRoadbooks(mapRoadbooks));
    }

    /**
     * 删除路书
     */
    @Operation(summary = "删除路书")
    @PreAuthorize("@ss.hasPermi('work:roadbooks:remove')")
    @Log(title = "路书", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mapRoadbooksService.deleteMapRoadbooksByIds(ids));
    }

    /**
     * 导入路书
     */
    @Operation(summary = "导入路书")
    @Log(title = "路书", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(@RequestBody Map<String,String> data) throws Exception
    {
        boolean updateSupport = true;
        String filePath = RuoYiConfig.getProfile()+data.get("path").toString().replaceAll("/profile","");
        String message = "导入失败";
        try (InputStream input = new FileInputStream(filePath)) {
            // 操作流
            ExcelUtil<MapRoadbooks> util = new ExcelUtil<MapRoadbooks>(MapRoadbooks.class);
            List<MapRoadbooks> mapRoadbooksList = util.importExcel(input);
            message = mapRoadbooksService.importMapRoadbooks(mapRoadbooksList, updateSupport);
        }catch (Exception e) {
            message = "打开上传文件失败";
        }
        
        return AjaxResult.success(message);
    }
}
